package com.graduation_design.service.impl;

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduation_design.entity.AppComment;
import com.graduation_design.entity.AppOrder;
import com.graduation_design.entity.AppOrderItem;
import com.graduation_design.entity.AppProduct;
import com.graduation_design.service.*;
import com.graduation_design.mapper.AppCommentMapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
* @author 从南
* @description 针对表【app_comment】的数据库操作Service实现
* @createDate 2024-02-16 18:15:58
*/
@Service
public class AppCommentServiceImpl extends ServiceImpl<AppCommentMapper, AppComment>
    implements AppCommentService{

    @Resource
    AppCommentMapper appCommentMapper;

    @Resource
    AppUserService appUserService;

    @Resource
    AppOrderService appOrderService;

    @Resource
    AppOrderItemService appOrderItemService;

    @Resource
    AppProductService appProductService;

    /**
     * 商品评价列表
     */
    @Override
    public Page<AppComment> pageByProductId(Page page, @Param("product") Long productId) {
        return appCommentMapper.pageByProductId(page, productId);
    }

    /**
     * 待评价
     * @param page
     * @param status 0 未评论，1 已评论
     * @return
     */
    @Override
    public Object getPage(Page page, Integer status) {

        long userId = appUserService.getCurrentUserId();

        // 已完成的订单列表的id集合
        List<Object> orderIds = appOrderService.listObjs(new QueryWrapper<AppOrder>()
                .eq("user_id", userId)
                .eq("order_status", 3)
                .select("id")
        );

        // 再获取已完成订单列表的所有的带评论的商品
        Page<AppOrderItem> orderItems = page;
        if (orderIds.size() > 0) {
            orderItems = appOrderItemService.page(page, new QueryWrapper<AppOrderItem>()
                    .isNull(status == 0, "comment_id")
                    .isNotNull(status == 1, "comment_id")
                    .in("order_id", orderIds)
                    .orderByDesc("created")
            );
        }

        return orderItems;
    }

    /**
     * 发表评价
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public long post(AppComment comment) {

        AppOrderItem orderItem = appOrderItemService.getById(comment.getOrderItemId());
        Assert.isNull(orderItem.getCommentId(), "请勿重复评论");

        AppOrder appOrder = appOrderService.getOwnById(orderItem.getOrderId());
        Assert.isTrue(appOrder.getOrderStatus() == 3, "该订单无法评论");

        comment.setOrderId(orderItem.getOrderId());
        comment.setProductId(orderItem.getProductId());
        comment.setUserId(appUserService.getCurrentUserId());
        comment.setReply(null);
        comment.setCreated(LocalDateTime.now());
        this.save(comment);

        orderItem.setCommentId(comment.getId());
        orderItem.setCommentTime(LocalDateTime.now());
        appOrderItemService.updateById(orderItem);

        // 商品评论数量 + 1
        appProductService.update(new UpdateWrapper<AppProduct>()
                .eq("id", orderItem.getProductId())
                .setSql("comments = comments + 1")
        );
        return comment.getId();
    }
}




